package com.aura.bookshop.controller;


import java.util.HashMap;
import java.util.Map;


import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.aura.bookshop.entity.Teacher;
import com.aura.bookshop.mapper.TeacherMapper;
import com.aura.bookshop.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jinbanglong
 * @since 2020-09-09
 */
@Slf4j
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     * @param pageNum
     * @param map
     * @return
     */
    @RequestMapping("/list/{pageNum}")
    public String getTeaList(@PathVariable("pageNum") Integer pageNum, Map<String, Object> map){
        /**
         * 单表分页查询
         */
        IPage<Teacher> teaPage = teacherService.page(new Page<Teacher>(pageNum, 3)
                , new QueryWrapper<Teacher>()
                    .gt("teacher_id", 0));


        System.out.println("teaPage");
        System.out.println(teaPage.getRecords());
        System.out.println(teaPage.getSize());
        System.out.println(teaPage.getTotal());
        System.out.println(teaPage.getPages());

        map.put("page", teaPage);

        //2 个表联合查询
        IPage<Teacher> myTeaPage = teacherService.selectMyPage(new Page<Teacher>(pageNum, 2)
                , new QueryWrapper<Teacher>()
                    .gt("teacher_id", 0));
        map.put("myTeaPage", myTeaPage);

        return "teaList";

    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("teaAdd")
    public String teaAdd(){

        return "teaAdd";
    }

    /**
     * 执行添加
     * @param teacher
     * @return
     */
    @PostMapping("add_do")
    public String addDo(Teacher teacher){

        boolean save = teacherService.save(teacher);


        return "redirect:/teacher/list/1";
    }

    /**
     * 修改页面
     * @return
     */
    @RequestMapping("/update/{teaId}")
    public String teaUpdate(@PathVariable String teaId, Map<String, Object> map){
        Teacher teacher = teacherService.getById(teaId);
        map.put("tea", teacher);
        return "teaUpdate";
    }
    /**
     * 修改执行
     * @return
     */
    @RequestMapping("/updateDo")
    public String updateDo(Teacher teacher){
        boolean b = teacherService.updateById(teacher);
        if(b){
            return "redirect:/teacher/list/1";
        }
        System.out.println("teacher");
        System.out.println(teacher);
        return "redirect:/teacher/update/"+teacher.getTeacherId();
    }






    /**
     * 执行删除
     * @param teaId
     * @return
     */
    @PostMapping("/delete/{teaId}")
    public String delDo(@PathVariable String teaId){

        boolean del = teacherService.removeById(teaId);

        return "redirect:/teacher/list/1";
    }


}

